const { pinyin } = require('pinyin-pro');
const fs = require('fs');
const path = require('path');
const country_cn = require('./hubei.ts');

const travalAndFormat = (addrList) => {
  for (let i = 0; i < addrList.length; i++) {
    let currentAddr = addrList[i];
    if (currentAddr.name && !currentAddr.initials) {
      currentAddr.initials = pinyin(currentAddr.name, { pattern: 'first', toneType: 'none', type: 'string' });
    }
    if (currentAddr.children) travalAndFormat(currentAddr.children);
  }
  fs.writeFile(path.resolve(`hubei_next.ts`), JSON.stringify(addrList), (err) => {
    if (err) throw err;
  });
};

const travalAndReset = (addrList) => {
  for (let i = 0; i < addrList.length; i++) {
    let currentAddr = addrList[i];
    if (currentAddr.name) {
      currentAddr.n = currentAddr.name;
      currentAddr.c = currentAddr.code;
      delete currentAddr.name;
      delete currentAddr.code;
      if (currentAddr.parent_code) delete currentAddr.parent_code;
    }
    if (currentAddr.children) travalAndReset(currentAddr.children);
  }
  fs.writeFile(path.resolve(`hubei_next.ts`), JSON.stringify(addrList), (err) => {
    if (err) throw err;
  });
};

travalAndReset(country_cn);
